
    /********************************************************/
    /*                                                      */
    /*                                                      */
    /*         Copyright (C), 1993  SEIKO EPSON Corp.       */
    /*         ALL RIGHTS RESERVED                          */
    /*                                                      */
    /*                                                      */
    /*  file name : ldiv.c                                  */
    /*                                                      */
    /*  Revision history                                    */
    /*      93/12/17    first release       S.Akaiwa        */
    /*      97/2/20	modified	    T.Katahira	    */
    /*			change function declaration	    */
    /*					to the ANSI style   */
    /*                                                      */
    /********************************************************/

#ifdef      UNIX
#include    "stdlib.h"
#include    "errno.h"
#else
#include    <stdlib.h>
#include    <errno.h>
#endif

ldiv_t
ldiv(long lNum, long lDen)

    /********************************************************/
    /*                                                      */
    /*  The ldiv function calculate the quotient and        */
    /*  remainder of the division of the long integer       */
    /*  numerator lNum by the long integer denominator lDen.*/
    /*  If 0 is specified by lDen, quotient and remainder   */
    /*  are set to 0 and errno is set to EDOM.              */
    /*                                                      */  
    /*      RETURN  The quotient and remainder in a         */
    /*              structure                               */
    /*                                                      */  
    /********************************************************/

    /* long            lNum    ;                  numerator */
    /* long            lDen    ;                denominator */
    {

     long           lAbsNum ;         /* absolute numerator */
     long           lAbsDen ;       /* absolute denominator */
     ldiv_t         stAns   ;           /* result structure */
     
     if(lDen == 0){                    /* check denominator */
        errno = EDOM;                     
        stAns.quot = 0;
        stAns.rem = 0;
        return stAns;                     /* divide by zero */
     }

     lAbsNum = labs(lNum);        /* get absolute numerator */
     lAbsDen = labs(lDen);      /* get absolute denominator */

     stAns.quot = lAbsNum / lAbsDen;  /* calculate quotient */
     stAns.rem = lAbsNum % lAbsDen;  /* calculate remainder */
     
     if(((lNum < 0) && (lDen > 0)) || ((lDen < 0) && (lNum > 0))){
        stAns.quot = -stAns.quot;   /* get sign of quotient */
     }
     
     if(lNum < 0){
        stAns.rem = -stAns.rem;    /* get sign of remainder */
     }

     return stAns;                         /* return result */
    }

